import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_1800/models/user.dart';
import 'package:flutter_1800/pages/user/updateNickName.dart';
import 'package:flutter_1800/pages/widgets/MyText.dart';
import 'package:flutter_1800/pages/widgets/avatar.dart';
import 'package:flutter_1800/tools/AppUtil.dart';
import 'package:flutter_1800/tools/config.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:image_picker/image_picker.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../service/api_service.dart';

class MineInfo extends StatefulWidget {
  const MineInfo({
    Key? key,
  }) : super(key: key);

  @override
  _MineInfoState createState() => _MineInfoState();
}

class _MineInfoState extends State<MineInfo>
    with AutomaticKeepAliveClientMixin {
  var userList = [];
  var cats = [];
  User? user;
  final picker = ImagePicker();

  final RefreshController _refreshController =
      RefreshController(initialRefresh: false);

  @override
  void initState() {
    user = AppUtil.user;
    getUser();
  }

  @override
  bool get wantKeepAlive => true;

  void chooseAvatar(bool camera) async {
    XFile? pickedFile = await picker.pickImage(
        source: !camera ? ImageSource.gallery : ImageSource.camera,
        maxHeight: 600,
        maxWidth: 600);

    if (pickedFile == null) {
      return;
    }else{

    EasyLoading.show(status: "图片上传中.");

    File? file = File(pickedFile!.path);
    getUser();
    }
  }

  void getUser() async {
    await AppUtil.refreshUser();
    setState(() {
      user = AppUtil.user;
    });
  }

  void updateSex(num) async {

    user = AppUtil.user;
    user!.sex = num;
    print(user);
    await ApiService.updateUser(user!);
    setState(() {
      user = AppUtil.user;
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);

    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          elevation: 0.0,
          title: MyText("个人信息"),
        ),
        backgroundColor: AppConfig.grayBgColor,
        body: Container(
            margin: AppUtil.getCommonPadding(),
            child: SingleChildScrollView(
                child: Column(children: [
              const SizedBox(height: 10),
              ClipRRect(
                  borderRadius: BorderRadius.circular(10),
                  child: Container(
                    decoration: const BoxDecoration(color: Colors.white),
                    child: Column(
                      children: [
                        GestureDetector(
                            behavior: HitTestBehavior.opaque,
                            onTap: () {
                              chooseAvatar(false); // 相册
                            },
                            child: Container(
                              padding: const EdgeInsets.all(10),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      MyText("头像"),
                                      const SizedBox(height: 5),
                                      MyText(
                                        "7天内仅可修改一次头像",
                                        color: const Color(0xffF42648),
                                      )
                                    ],
                                  ),
                                  const Spacer(),
                                  Container(
                                    margin: const EdgeInsets.only(
                                        right: 10, top: 10, bottom: 10),
                                    width: 40,
                                    height: 40,
                                    child: ClipRRect(
                                      borderRadius: BorderRadius.circular(20),
                                      child: Avatar(user!.headimg),
                                    ),
                                  ),
                                  const Icon(Icons.navigate_next)
                                ],
                              ),
                            )),
                      ],
                    ),
                  )),
              const SizedBox(
                height: 10,
              ),
              ClipRRect(
                  borderRadius: BorderRadius.circular(10),
                  child: Container(
                    decoration: const BoxDecoration(color: Colors.white),
                    child: Column(
                      children: [
                        GestureDetector(
                            behavior: HitTestBehavior.opaque,
                            onTap: () {},
                            child: Container(
                              padding: const EdgeInsets.all(10),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  MyText("手机号"),
                                  MyText(user!.mobile),
                                ],
                              ),
                            )),
                        Container(height: 1, color: AppConfig.grayBgColor),
                        GestureDetector(
                            behavior: HitTestBehavior.opaque,
                            onTap: () {},
                            child: Container(
                              padding: const EdgeInsets.all(10),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  MyText("账号"),
                                  MyText(user?.account ?? "暂无账号"),
                                ],
                              ),
                            )),
                        Container(height: 1, color: AppConfig.grayBgColor),
                        GestureDetector(
                            behavior: HitTestBehavior.opaque,
                            onTap: () {
                              // AppUtil.getTo(const UpdateNickname());
                            },
                            child: Container(
                              padding: const EdgeInsets.all(10),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      MyText("昵称"),
                                      const SizedBox(height: 5),
                                      MyText(
                                        "7天内仅可修改一次昵称",
                                        color: const Color(0xffF42648),
                                      )
                                    ],
                                  ),
                                  const Spacer(),
                                  MyText(user?.nickname ?? "爱吃的小胖子"),
                                  const Icon(Icons.navigate_next)
                                ],
                              ),
                            )),
                        Container(height: 1, color: AppConfig.grayBgColor),
                        GestureDetector(
                            behavior: HitTestBehavior.opaque,
                            onTap: () {},
                            child: Container(
                              padding: const EdgeInsets.all(10),
                              child: Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  MyText("性别"),
                                  const Spacer(),
                                  GestureDetector(
                                    behavior: HitTestBehavior.opaque,
                                    onTap: () {
                                      setState(() {
                                        // user!.sex = 1;
                                        updateSex(1);
                                      });
                                    },
                                    child: Image.asset(
                                      user!.sex == 1
                                          ? "images/radio_checked.png"
                                          : "images/radio_unchecked.png",
                                      width: 20,
                                      height: 20,
                                    ),
                                  ),
                                  const SizedBox(
                                    width: 5,
                                  ),
                                  MyText("男"),
                                  const SizedBox(
                                    width: 10,
                                  ),
                                  GestureDetector(
                                    behavior: HitTestBehavior.opaque,
                                    onTap: () {
                                      setState(() {
                                        // user!.sex = 2;
                                        updateSex(2);
                                      });
                                    },
                                    child: Image.asset(
                                      user!.sex == 2
                                          ? "images/radio_checked.png"
                                          : "images/radio_unchecked.png",
                                      width: 20,
                                      height: 20,
                                    ),
                                  ),
                                  const SizedBox(
                                    width: 5,
                                  ),
                                  MyText("女"),
                                ],
                              ),
                            )),
                        Container(height: 1, color: AppConfig.grayBgColor),
                        GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          // onTap: () {
                          //   AppUtil.getTo(const UpdateSignature());
                          // },
                          child: Container(
                            padding: const EdgeInsets.all(10),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                MyText("个性签名"),
                                const Spacer(),
                                MyText(user!.signature ?? "这个人很懒，什么都没有留下"),
                                // const Icon(Icons.navigate_next)
                              ],
                            ),
                          ),
                        )
                      ],
                    ),
                  )),
            ]))));
  }
}
